
Skt.TeacherTracker = L.Class.extend({
	
	includes: L.Mixin.Events,
	
	events: ['update'],

	initialize: function(sketchpad) {
		this._teacherRoomId = null;
		this._followingEnabled = false;

		this._sketchpad = sketchpad;
		
		this._couple(this._sketchpad.getOptions().coupling);
	},
	
	setFollowingEnabled: function(enabled) {
		var self = this;
		self._followingEnabled = enabled;
		
		self.fire('update', {
			action: 'followingEnabledChanged',
			followingEnabled: self._followingEnabled,
			teacherRoomId: self._teacherRoomId,
			teacherTracker: self
		});
	},
	
	isFollowingEnabled: function() {
		var self = this;
		return self._followingEnabled;
	},
	
	setTeacherRoomId: function(roomId) {
		var self = this;
		self._teacherRoomId = roomId;
		
		self.fire('update', {
			action: 'teacherRoomChanged',
			followingEnabled: self._followingEnabled,
			teacherRoomId: self._teacherRoomId,
			teacherTracker: self
		});
	},
	
	getTeacherRoomId: function() {
		var self = this;
		return self._teacherRoomId;
	},
	
	toggleFollowingEnabled: function() {
		var self = this;
		self.setFollowingEnabled(!self.isFollowingEnabled());
	},
	
	getState: function() {
		var self = this;
		return {
			teacherRoomId: self._teacherRoomId,
			followingEnabled: self._followingEnabled			
		};
	},
	
	setState: function(state) {
		var self = this;
		self.setTeacherRoomId(state.teacherRoomId);
		self.setFollowingEnabled(state.followingEnabled);
	},
	
	_couple: function(coupling) {
		if ( coupling.enabled ) {
			coupling.activityAdapter.coupleObject(
				"teachertracker",
				this,
				{
					messageType: "STATE",
					explicitMapping: ["setTeacherRoomId", "setFollowingEnabled"]
				}
			);
		}
	}
	
});

Skt.TeacherTracker.findSelf = function() {
	return Skt.sketchpad.getTeacherTracker();
};
